package ConnectMysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class PreParedStatementTest {
	public static void main(String[] args) {
		Connection con = null;
		PreparedStatement ment = null;
		ResultSet set = null;

		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("加载驱动失败");
		}
		String url = "jdbc:mysql://localhost/huangbiao";
		String user = "root";
		String password = "root";
		try {
			con = DriverManager.getConnection(url, user, password);
			ment = con.prepareStatement("select *from exam2 where math=?");
			ment.setInt(1, 45);
			//ment.setInt(1, 23);
			set = ment.executeQuery();
			while (set.next()) {
				System.out.print("id为" + set.getString("id"));
				System.out.print("成绩为" + set.getString("math"));
				System.out.print("性别为" + set.getString("sex"));
				System.out.println("喜欢的明星为" + set.getString("sname"));

			}
		} catch (SQLException e) {
			System.out.println("数据库连接查询失败");
		}

	}
}
